#ifndef CONFIGITEM_H
#define CONFIGITEM_H
#include <QObject>
#include <QSet>
#include <QDebug>
#include "UpdateOption.h"

class ConfigEditor;
class ConfigDataModel;

class ConfigItem : public QObject {
    Q_OBJECT

    friend class ConfigDataModel;

    friend QDataStream & operator<< (QDataStream& stream, const ConfigItem& item);
    friend QDataStream & operator>> (QDataStream& stream, ConfigItem& item);
    friend QDataStream & operator<< (QDataStream& stream, const ConfigDataModel& item);
    friend QDataStream & operator>> (QDataStream& stream, ConfigDataModel& item);

public:
    enum WidgetStyle { Default, NoWidget, RadioButton, ComboBox, ComboBoxCompleted, CheckBox };
    QString translatedName;
    ConfigItem();
    virtual QWidget* createRelatedWidget();
    virtual QWidget* createRelatedWidget(WidgetStyle s);
    WidgetStyle getWidgetStyle();
    void setWidgetStyle(WidgetStyle s);

    virtual void updateItem(QWidget* widget, UpdateOption option);

protected:
    WidgetStyle m_widgetStyle = WidgetStyle::Default;

    virtual void readFromData(QDataStream &stream) = 0;
    virtual void writeToData(QDataStream &stream) const = 0;

    virtual QWidget* createWidget();
    virtual QWidget* createWidget(WidgetStyle s) = 0;
    virtual void update(QWidget* widget, UpdateOption option) = 0;

    QSet<QWidget *> relatedWidgets;

private:
    void relate(QWidget *w);

protected slots:
    void onItemEdited();
    void onWidgetDestroyed(QObject *obj);
    void onWidgetEdited(QWidget *w);
    void onWidgetEditedWrapper();

signals:
    void itemEdited();
    void widgetEdited(QWidget *w, QPrivateSignal );

};

QDataStream & operator<< (QDataStream& stream, const ConfigItem& item);
QDataStream & operator>> (QDataStream& stream, ConfigItem& item);

#endif // CONFIGITEM_H
